# coding=utf-8
from google.appengine.ext import db

#分类
class Category(db.Model):
	name = db.StringProperty( required = True)
	order = db.IntegerProperty(required = True, default = 0)
	
#日志
class Link(db.Model):
	url = db.URLProperty(required = True)
	title = db.StringProperty(required = True)
	order = db.IntegerProperty(required = True, default = 0)
	
#日志
class Diary(db.Model):
	title = db.StringProperty()
	content = db.TextProperty()
	post_time = db.DateTimeProperty( auto_now_add = True)
	last_modify_time = db.DateTimeProperty( auto_now = True )
	author = db.UserProperty(auto_current_user=True)
	published = db.BooleanProperty( required = True, default = False )
	read_count = db.IntegerProperty( required = True, default = 0 )
	category = db.ReferenceProperty(Category)
	
	@property
	def tags(self):
		tags = [tag.name for tag in Tag.gql("WHERE diaries = :1", self.key())]
		return ' '.join(tags)
	
	def tags_set(self):
		return [tag for tag in Tag.gql("WHERE diaries = :1", self.key())]

#标签
class Tag(db.Model):
	name = db.StringProperty( required = True )
	diaries = db.ListProperty(db.Key)

#评论
class Comment(db.Model):
	content = db.StringProperty( multiline=True )
	author = db.UserProperty(auto_current_user=True)
	nick_name = db.StringProperty()
	email = db.EmailProperty()
	post_time = db.DateTimeProperty( auto_now_add = True )
	diary = db.ReferenceProperty(Diary)
    